<?
class BadWord{
	
    //check_badword
    static function get_badword($delcache = false) {
        $badword = array();
        $subDir = 'badword';
        $badword = ECCache::cache('SELECT * FROM bad_words ORDER By exact DESC', __LINE__ . __FILE__, 86400, false, '', $subDir, $delcache);
        usort($badword, array('BadWord', 'word_length_sort'));
        if ($badword && !$delcache) {
            return $badword;
        }
        return $badword;
    }

    static function word_length_sort($a, $b) {
        if (mb_strlen($a['contents'], 'UTF-8') == mb_strlen($b['contents'], 'UTF-8')) {
            return 0;
        }
        return ( mb_strlen($a['contents'], 'UTF-8') > mb_strlen($b['contents'], 'UTF-8') ) ? -1 : 1;
    }

    static function checkBadWord($str_check='', $return = false, $del_cache = false, $getReason = false)
    {
        if ($str_check == "" && !$del_cache)
        {
            return false;
        }

        for ($i = 65; $i <= 90; $i++) {
            $str_check = str_replace("&#" . $i . ";", chr($i), $str_check);
        }

        for ($i = 97; $i <= 122; $i++) {
            $str_check = str_replace("&#" . $i . ";", chr($i), $str_check);
        }

        $str_check = preg_replace("/<br[^>]*>/i", "\n", $str_check);
        $str_check = preg_replace("/<p[^>]*>/i", "\n", $str_check);
        $str_check = preg_replace("/<\/p[^>]*>/i", "\n", $str_check);

        $str_check = strip_tags($str_check);

        $str_check = str_replace(chr(9), ' ', $str_check);

        $str_check = str_replace("&nbsp;", " ", $str_check);

        $matches = array();
        $arr_badword = BadWord::get_badword();

        if (!$del_cache) {

            foreach ($arr_badword as $badword) {
                $realBad = str_replace('*', '', $badword['contents']);
                $bad = preg_quote($badword['contents']);
                $badword['contents'] = preg_quote($badword['contents']);
                $badword['contents'] = str_replace(array('\*', '\?'), array('(.{0,3})', '(.+)'), $badword['contents']);

                if ($badword['exact']) {
                    if (preg_match('#(^|\s|\b)' . $badword['contents'] . '(\b|\s|!|\?|\.|,|$)#ui', $str_check, $match)) {
                        if ($return) {
                            $bad_arrs[$bad] = $bad;
                            $matches[] = $match[0];
                            $reason_arrs[$realBad] = $badword['reason'];
                        } else {
                            return true;
                        }
                    }
                } else {
                    if (preg_match('#' . $badword['contents'] . '#ui', $str_check, $match)) {
                        if ($return) {
                            $bad_arrs[$bad] = $bad;
                            $matches[] = $match[0];
                            $reason_arrs[$realBad] = $badword['reason'];
                        } else {
                            return true;
                        }
                    }
                }
            }

            if ($return && isset($bad_arrs)) {
                $arrReturn = array(
                    'bad' => implode(', ', $matches),
                    'bad_key' => str_replace(array('\*', '\?'), '', implode(', ', $bad_arrs))
                );
                if ($getReason) {

                    $arrReturn += array(
                        'reason' => $reason_arrs
                    );
                }
                return $arrReturn;
            } else {
                return false;
            }
        }
    }
    
	static function check_badwords($str_check='', $return = false, $del_cache = false)
	{
		if ($str_check == "" && !$del_cache)
		{
			return false;
		}

		##################################################################
		//xử lý lọc nội dung href của thẻ a
		$aryResult = array();
		$result = array();
		preg_match_all("#<a[^>]*href=[\"\']([^\"^\']*)#", stripslashes($str_check), $aryResult);
		if(isset($aryResult[1]))
		{
			$result = $aryResult[1];
		}	
			
		for( $i = 65; $i <= 90; $i++ )
		{
			$str_check = str_replace( "&#".$i.";", chr($i), $str_check );
		}
		
		for( $i = 97; $i <= 122; $i++ )
		{
			$str_check = str_replace( "&#".$i.";", chr($i), $str_check );
		}
		
		$str_check = preg_replace("/<br[^>]*>/", "\n", $str_check);
		$str_check = preg_replace("/<p[^>]*>/", "\n", $str_check);
    	$str_check = preg_replace("/<p[^>]*>/", "\n", $str_check);
		$str_check = strip_tags($str_check);
		$str_check = str_replace(chr(9), ' ', $str_check);  
		$str_check = str_replace("&nbsp;", " ", $str_check);
		
		//$sql = "SELECT * FROM phpclass_badwords WHERE is_phone_no = 0 ORDER BY type";				
		$sql = "SELECT * FROM phpclass_badwords ORDER BY type";				
		
		$matches=array();
		$bad_arrs=array();
		$check  =0;
		$arr_badword = ECCache::cache($sql,__LINE__.__FILE__,3600*24,false,'','',$del_cache);			
		
		if(!$del_cache)
		{
			foreach($arr_badword as $badword)
			{
				$bad_url = stripslashes($badword['contents']);
				$bad 				 = preg_quote($badword['contents']);
				$badword['contents'] = preg_quote($badword['contents']);
				$badword['contents'] = str_replace( array('\*','\?'), array('(.{0,3})','(.+)') , $badword['contents'] );			
					
				if($badword['exact']) //so sanh chinh xac noi dung href trong the a voi badword
				{					
					if(in_array($bad_url, $result))
					{
						$bad_arrs[$bad_url]=$bad_url;
						$matches[]=$bad_url;
						$check=$check!=2?($badword['type']==1?3:2):$check;
					}
					
					if(@preg_match( '#(^|\s|\b)'.$badword['contents'].'(\b|\s|!|\?|\.|,|$)#ui', $str_check,$match))
					{
						if($return)
						{
							$bad_arrs[$bad]=$bad;
							$matches[]=$match[0];
							$check=$check!=2?($badword['type']==1?3:2):$check;
						}
						else
						{
							return $badword['type']==1?3:2;
						}
					}	
				}
				else  //so sanh tuong doi noi dung href trong the a voi badword 
				{ 					
					if(count($result)>0)
					{
						foreach($result as $res)
						{
							if(strlen(stristr($res,$bad_url)) > 0) // nếu link có chứa badword
							{
								$bad_arrs[$bad_url]=$bad_url;
								$matches[$bad_url]=$bad_url;
								$check=$check!=2?($badword['type']==1?3:2):$check;
							}
						}
					}
					if(@preg_match('#'.$badword['contents'].'#ui', $str_check, $match))
					{
						if($return)
						{
							$bad_arrs[$bad]=$bad;
							$matches[$bad]=$match[0];
							$check=$check!=2?($badword['type']==1?3:2):$check;
						}
						else
						{
							return $badword['type']==1?3:2;
						}
					}
				}
			}
			if($return && $check)
			{
				return array(
							'type'		=>$check,
							'bad' 		=>implode(', ',$matches),
							'bad_key' 	=>str_replace(array('\*','\?'),'',implode(', ',$bad_arrs))
							);
			}
			else
			{
				return false;
			}
		}
	}
	
	static function check_badwords_phone($str_check='', $return = false, $del_cache = false)
	{
		if ($str_check == "" && !$del_cache)
		{
			return false;
		}

		$sql = "SELECT * FROM phpclass_badwords WHERE is_phone_no = 1 ORDER BY type";
		
		$arr_badword = ECCache::cache($sql,__LINE__.__FILE__,3600*24,false,'','',$del_cache);
		
		if(!$del_cache)
		{
			foreach($arr_badword as $badword)
			{
				$badword["contents"] = str_replace('*','',$badword["contents"]);
				if(preg_match("#".$badword["contents"]."#i", $str_check))
				{
					if($return)
					{						
						$arr_rt = array(
							'id'		=> $badword['id'],
							'type'		=> $badword['type']==1?3:2,
							'bad' 		=> $str_check,
							'bad_key' 	=> $badword["contents"]
							);
													
						return $arr_rt;
						
					}
					else
					{
						return $badword['type']==1?3:2;
					}
				}
				
			}			
		}
	}		
}
?>